package com.zheng.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.zheng.mall.dao.PmsProductCategoryAttributeRelationDao;
import com.zheng.mall.dao.PmsProductCategoryDao;
import com.zheng.mall.dto.PmsProductCategoryParam;
import com.zheng.mall.dto.PmsProductCategoryWithChildrenItem;
import com.zheng.mall.mapper.PmsProductCategoryAttributeRelationMapper;
import com.zheng.mall.mapper.PmsProductCategoryMapper;
import com.zheng.mall.mapper.PmsProductMapper;
import com.zheng.mall.model.*;
import com.zheng.mall.service.PmsProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类Service
 * @author zheng
 */
@Service
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationDao productCategoryAttributeRelationDao;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;
    @Autowired
    private PmsProductCategoryDao productCategoryDao;

    /**
     * 创建商品分类
     *
     * @param pmsProductCategoryParam
     */
    @Override
    public int create(PmsProductCategoryParam pmsProductCategoryParam) {
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        pmsProductCategory.setProductCount(0);
        BeanUtils.copyProperties(pmsProductCategoryParam, pmsProductCategory);
        //没有父级分类时，为1级分类
        setCategoryLevel(pmsProductCategory);
        int count = productCategoryMapper.insertSelective(pmsProductCategory);
        //创建筛选属性关联
        List<Long> attributeIdList = pmsProductCategoryParam.getProductAttributeIdList();
        if (!CollectionUtils.isEmpty(attributeIdList)) {
            insertRelationList(pmsProductCategory.getId(), attributeIdList);
        }
        return count;
    }

    /**
     * 修改商品分类
     *
     * @param id
     * @param pmsProductCategoryParam
     */
    @Override
    public int update(Long id, PmsProductCategoryParam pmsProductCategoryParam) {
        PmsProductCategory category = new PmsProductCategory();
        category.setId(id);
        BeanUtils.copyProperties(pmsProductCategoryParam, category);
        setCategoryLevel(category);
        //更新商品分类时要更新商品中的名称
        PmsProduct product = new PmsProduct();
        product.setProductCategoryName(category.getName());
        PmsProductExample productExample = new PmsProductExample();
        productExample.createCriteria().andProductCategoryIdEqualTo(id);
        productMapper.updateByExampleSelective(product, productExample);
        //更新筛选属性信息
        if (!CollectionUtils.isEmpty(pmsProductCategoryParam.getProductAttributeIdList())) {
            PmsProductCategoryAttributeRelationExample relationExample = new PmsProductCategoryAttributeRelationExample();
            relationExample.createCriteria().andProductCategoryIdEqualTo(id);
            productCategoryAttributeRelationMapper.deleteByExample(relationExample);
            insertRelationList(id, pmsProductCategoryParam.getProductAttributeIdList());
        } else {
            PmsProductCategoryAttributeRelationExample relationExample = new PmsProductCategoryAttributeRelationExample();
            relationExample.createCriteria().andProductCategoryIdEqualTo(id);
            productCategoryAttributeRelationMapper.deleteByExample(relationExample);
        }
        return productCategoryMapper.updateByPrimaryKeySelective(category);
    }

    /**
     * 分页获取商品分类
     *
     * @param parentId
     * @param pageSize
     * @param pageNum
     */
    @Override
    public List<PmsProductCategory> getList(Long parentId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PmsProductCategoryExample categoryExample = new PmsProductCategoryExample();
        categoryExample.setOrderByClause("sort desc");
        categoryExample.createCriteria().andParentIdEqualTo(parentId);
        return productCategoryMapper.selectByExample(categoryExample);
    }

    /**
     * 删除商品分类
     *
     * @param id
     */
    @Override
    public int delete(Long id) {
        return productCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据ID获取商品分类
     *
     * @param id
     */
    @Override
    public PmsProductCategory getItem(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 批量修改导航状态
     *
     * @param ids
     * @param navStatus
     */
    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        PmsProductCategory category = new PmsProductCategory();
        category.setNavStatus(navStatus);
        PmsProductCategoryExample categoryExample = new PmsProductCategoryExample();
        categoryExample.createCriteria().andIdIn(ids);
        return productCategoryMapper.updateByExample(category, categoryExample);
    }

    /**
     * 批量修改显示状态
     *
     * @param ids
     * @param showStatus
     */
    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsProductCategory category = new PmsProductCategory();
        category.setShowStatus(showStatus);
        PmsProductCategoryExample categoryExample = new PmsProductCategoryExample();
        categoryExample.createCriteria().andIdIn(ids);
        return productCategoryMapper.updateByExampleSelective(category, categoryExample);
    }

    /**
     * 以层级形式获取商品分类
     */
    @Override
    public List<PmsProductCategoryWithChildrenItem> listWithChildren() {
        return productCategoryDao.listWithChildren();
    }

    /**
     * 根据分类的parentId设置分类的level
     * @param pmsProductCategory 商品分类
     */
    private void setCategoryLevel (PmsProductCategory pmsProductCategory) {
        //没有父级分类时，为一级分类
        if (pmsProductCategory.getParentId() == 0){
            pmsProductCategory.setLevel(0);
        }else {
            //有父级分类时，根据父级分类设置
            PmsProductCategory parentCategory = productCategoryMapper.selectByPrimaryKey(pmsProductCategory.getParentId());
            if (parentCategory != null) {
                pmsProductCategory.setLevel(parentCategory.getLevel() + 1);
            }else {
                pmsProductCategory.setLevel(0);
            }
        }
    }

    /**
     * 批量插入商品分类与筛选属性关系表
     * @param productCategoryId 商品分类id
     * @param productAttributeIdList 相关商品筛选属性id集合
     */
    private void insertRelationList (Long productCategoryId, List<Long> productAttributeIdList) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long productAttrId : productAttributeIdList) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(productCategoryId);
            relation.setProductCategoryId(productCategoryId);
            relationList.add(relation);
        }
        productCategoryAttributeRelationDao.insertList(relationList);
    }
}
